﻿using System;
using System.ServiceModel.Activation;
using Prompts.Service.ReportExecution;

namespace Prompts.Service.PromptService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ChildPromptLevelService : IChildPromptLevelService
    {
        private readonly IHierarchyPromptService _hierarchyPromptService;
        private readonly IHierarchyPromptService _recursiveHierarchyPromptService;

        public ChildPromptLevelService(
            IHierarchyPromptService hierarchyPromptService,
            IHierarchyPromptService recursiveHierarchyPromptService)
        {
            _recursiveHierarchyPromptService = recursiveHierarchyPromptService;
            _hierarchyPromptService = hierarchyPromptService;
        }

        public GetChildrenResponse GetChildren(string promptName, string parameterName, ParameterValue[] parameterValues)
        {
            try
            {
                var hierarhcy = _hierarchyPromptService.GetHierarchyPrompt(promptName, parameterValues);
                var promptLevel = hierarhcy.GetChildOf(parameterName);
                return new GetChildrenResponse { ErrorMessage = string.Empty, ErrorOccured = false, PromptLevel = promptLevel };
            }
            catch (Exception exception)
            {
                return new GetChildrenResponse {ErrorMessage = exception.Message, ErrorOccured = true};
            }
        }

        public GetChildrenResponse GetChildren2(string promptName, string parameterName, ParameterValue parameterValue)
        {
            return GetChildren(promptName, parameterName, new[] {parameterValue});
        }

        public GetChildrenResponse GetChildrenForRecursive(string promptName, string parameterName, ParameterValue parameterValue)
        {
            try
            {
                var hierarhcy = _recursiveHierarchyPromptService.GetHierarchyPrompt(promptName, new[] { parameterValue });
                var promptLevel = hierarhcy.GetChildOf(parameterName);
                return new GetChildrenResponse { ErrorMessage = string.Empty, ErrorOccured = false, PromptLevel = promptLevel };
            }
            catch (Exception exception)
            {
                return new GetChildrenResponse { ErrorMessage = exception.Message, ErrorOccured = true };
            }
        }
    }
}
